import Base from 'models/base';

import moment from 'moment';

export default class CouponForm extends Base {
  constructor(coupon) {
    super();

    if (coupon === undefined) {
      coupon = {
        combineUse: 0
      };
      this.startTime = null;
      this.endTime = null;
      this.maxCount = undefined;
      this.couponAmount = undefined;
      this.transactionMinimum = undefined;
      this.maxCouponsPerUser = undefined;
      this.maxCoupons = undefined;

      this.branchId = '';
      this.partnerId = '';
    } else {
      this.id = coupon.id;
      let startTime = coupon.availableBeginTime;
      this.startTime = moment(startTime).valueOf();

      let endTime = coupon.availableEndTime;
      this.endTime = moment(endTime).valueOf();

      let couponAmount = coupon.couponAmount;
      this.couponAmount = couponAmount;
      this.transactionMinimum = coupon.transactionMinimum;
      this.maxCouponsPerUser = coupon.maxCouponsPerUser;
      this.maxCoupons = coupon.maxCoupons;

      let maxAmountByDay = coupon.maxAmountByDay;
      let maxCount = _.round(maxAmountByDay / couponAmount, 0);
      this.maxCount = maxCount;

      this.shopName = coupon.bpName;
    }

    let fields = ['stockId', 'stockName', 'description', 'combineUse', 'status', 'stopStatus'];
    this.initObject(coupon, fields);
  }

  checkValid(user, nowTime) {
    let fields = ['stockName', 'couponAmount', 'transactionMinimum', 'maxCoupons', 'maxCount', 'startTime', 'endTime', 'description',
      'maxCouponsPerUser'
    ];
    let code = this.checkRequired(fields);
    if (code !== '') {
      return code;
    }

    if (user.branchId === 1) {
      if (this.branchId === '') {
        return 'branchId';
      }
    }

    if (this.transactionMinimum < this.couponAmount) {
      return 'transactionMinimum<couponAmount';
    }

    let maxCoupons = this.maxCoupons;
    let maxCount = this.maxCount;
    if (maxCount > maxCoupons) {
      return 'maxCount>maxCoupons';
    }

    let startTime = this.startTime;
    if (moment(nowTime).isAfter(startTime, 'minute')) {
      return 'nowTime>startTime';
    }

    let endTime = this.endTime;
    if (moment(endTime).isBefore(startTime, 'minute')) {
      return 'endTime<startTime';
    }

    let maxCouponsPerUser = this.maxCouponsPerUser;
    if (maxCouponsPerUser > maxCount) {
      return 'maxCouponsPerUser>maxCount';
    }

    return '';
  }

  getObject() {
    let postData = {};
    let fields = ['stockName', 'couponAmount', 'transactionMinimum', 'maxCoupons', 'description', 'maxCouponsPerUser', 'combineUse'];
    this.setPostData(postData, fields);

    let branchId = this.branchId;
    if (branchId !== '') {
      let partnerId = this.partnerId;
      if (partnerId === '') {
        postData.branchIdList = [branchId];
      } else {
        postData.partnerIdList = [partnerId];
      }
    }

    let totalMoney = this.couponAmount * this.maxCount;
    postData.maxAmountByDay = totalMoney;

    let startTime = this.startTime;
    postData.availableBeginTime = moment(startTime).format('YYYY-MM-DDTHH:mm:00.000+08:00');

    let endTime = this.endTime;
    postData.availableEndTime = moment(endTime).format('YYYY-MM-DDTHH:mm:00.000+08:00');

    return postData;
  }
}
